package com.yydm.serviceacls.controller;

import com.yydm.commonutils.R;
import com.yydm.serviceacls.entity.User;
import com.yydm.serviceacls.service.LoginServer;
import com.yydm.serviceacls.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;


/**
 * 登录退出注册功能
 */
@RestController
@RequestMapping("/acl/user")
public class LoginController {
    @Autowired
    private LoginServer loginServer;

    @Autowired
    private UserService userServer;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody User user) {
        return loginServer.login(user);
    }

    /**
     * 退出
     * @return
     */
    @GetMapping("/logout")
    public R logout(){
        return loginServer.logout();
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("/registered")
    public R registered(@RequestBody User user){
        if (user != null) {
            String encode = passwordEncoder.encode(user.getPassword());
            user.setPassword(encode);
            user.setImage("666.jpg");
            userServer.save(user);
            return R.ok().message("增加成功");
        } else {
            return R.error().message("增加失败");
        }
    }

}
